package com.googlecode.cryptoid;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;
import java.util.Stack;

import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MatrixList extends ListActivity {
	@Override
	public void onResume() {
	  super.onResume();
	  
	  final Intent intent = new Intent(this, EditMatrix.class);
	  final String[] matrices = getMatrixList();
	  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, matrices));

	  ListView lv = getListView();
	  lv.setTextFilterEnabled(true);

	  lv.setOnItemClickListener(new OnItemClickListener() {
	    public void onItemClick(AdapterView<?> parent, View view,
	        int position, long id) {
	      if(position == 0){
	    	  Matrix.createUnit();
	    	  EditMatrix.newMatrix = true;
	    	  startActivity(intent);
	    	  finish();
	      }
	      else if(fileToMatrix(matrices[position])){
	    	  startActivity(intent);
	    	  finish();
	      }
	      else
	    	  Toast.makeText(getApplicationContext(),  R.string.couldntread, Toast.LENGTH_SHORT).show();
	    }
	  });
	  
	}

	private String[] getMatrixList() {
		boolean inputReady = true;
		int len = 0;
		Stack<String> namesStack = new Stack<String>();
		String[] initialList = {getString(R.string.new_)};
	
		
			ObjectInputStream input;
			try {
				input = new ObjectInputStream(openFileInput(".mtxlist"));
				if(inputReady){
					String[] namesIn = (String[]) input.readObject();
					input.close();
				
					len = namesIn.length;
					
					for(int i = 1; i <= len; i++)
						namesStack.add(namesIn[len - i]);
					
				}
			}
			  catch (FileNotFoundException e) {
				return initialList;
			} catch (StreamCorruptedException e) {
				return initialList;
			} catch (IOException e) {
				return initialList;
			} catch (ClassNotFoundException e) {
				return initialList;
			} 
		
			String[] names = new String[len+1];
			
			names[0] = "New...";
			for(int i = 1; i < len+1; i ++)
				names[i] = namesStack.pop();
			
			return names;
	}

	private boolean fileToMatrix(String matrixName){
		try {
			ObjectInputStream input = new ObjectInputStream(openFileInput(matrixName + ".mtx"));
			
			int[][][] dataArray = (int[][][]) input.readObject();
			Matrix.matrix = dataArray[0];
			int [][] dummyArray = dataArray[1];
			int [] sizeArray = dummyArray[0];
			Matrix.size = sizeArray[0];
			Matrix.name = matrixName;
			
			input.close();
			return true;
			
		} catch (StreamCorruptedException e) {
			Toast.makeText(this, R.string.couldntread ,Toast.LENGTH_SHORT).show();
			return false;
		} catch (FileNotFoundException e) {
			Toast.makeText(this, R.string.couldntread ,Toast.LENGTH_SHORT).show(); 
			return false;
		} catch (IOException e) {
			Toast.makeText(this, R.string.couldntread ,Toast.LENGTH_SHORT).show();
			return false;
		} catch (ClassNotFoundException e) {
			Toast.makeText(this, R.string.couldntread ,Toast.LENGTH_SHORT).show();
			return false;
		}
		
	}
	
	
	
	
}
